<template>
  <!-- 退款记录 -->
  <div class="flexContent">
    <div class="searchBox">

      <el-form ref="formRef" :model="sForm" class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="门店名称">
                  <el-select v-model="sForm.storeId" popper-class="eloption" clearable style="width: 95%;" filterable
                    placeholder="请选择" @click.native="getShopEasy">
                    <el-option v-for="brand in brandList" :key="brand.id" :value="brand.id" :label="brand.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="查询编号">
                  <el-input v-clearEmoji v-model.trim="sForm.returnsNumber" placeholder="请输入退款编号/订单编号" clearable
                    style="width: 95%;" @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="退款状态">
                  <el-select v-model="sForm.status" popper-class="eloption" clearable style="width: 95%;"
                    placeholder="请选择">
                    <el-option value="0" label="退款中" />
                    <el-option value="1" label="成功" />
                    <el-option value="2" label="失败" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="退款时间">
                  <el-date-picker clearable v-model="date" style="width: 95%;" type="daterange" unlink-panels
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="storeName" title="门店名称" :min-width="elTableAdaptationWidth('storeName', accountList)" />
          <vxe-column field="returnsNumber" title="退货编号"
            :min-width="elTableAdaptationWidth('returnsNumber', accountList)" />
          <vxe-column field="orderNumber" title="订单编号" :min-width="elTableAdaptationWidth('orderNumber', accountList)" />
          <vxe-column field="returnsMoney" title="退货金额(总)" min-width="150" />
          <vxe-column field="returnsMode" title="退款方式" min-width="150">
            <template v-slot="{ row }">
              <span v-if="row.returnsMode == 0">收钱箱现金退款</span>
              <span v-if="row.returnsMode == 1">微信转账</span>
              <span v-if="row.returnsMode == 2">支付宝转账</span>
            </template>
          </vxe-column>
          <vxe-column field="status" title="状态" min-width="150">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0'" disabled type="primary" plain size="small">退款中</el-tag>
              <el-tag v-if="row.status === '1'" disabled type="success" plain size="small">成功</el-tag>
              <el-tag v-if="row.status === '2'" disabled type="danger" plain size="small">失败</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="createTime" title="退款时间" min-width="150">
            <template v-slot="{ row }">
              {{ row.createTime | parseTime }}
            </template>
          </vxe-column>
          <vxe-column v-if="$showFn(['reimburse_detail'])" fixed="right" width="100" title="操作">
            <template v-slot="{ row }">
              <el-button type="text" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click="detail(row)">详情</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <el-dialog :visible.sync="dialogTableVisible" destroy-on-close width="50%" title="退款记录">
      <el-form class="form" v-model="dialogForm">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="门店名称：">
              {{ dialogForm.storeName }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="退款原因：">
              {{ returnsReason }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="退款时间：">
              {{ dialogForm.returnsTime | parseTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-table border :data="gridData" class="tablebox" :max-height="400" style="margin-bottom: 10px" size="medium">
          <el-table-column prop="commodityName" :min-width="elTableAdaptationWidth('commodityName', gridData) + 10"
            label="商品名称" align="center" />
          <el-table-column prop="commodityNum" label="商品类型" align="center" min-width="80">
            <template v-slot="{ row }">
              <span v-if="row.type === '0'">标品</span>
              <span v-if="row.type === '1'">非标品</span>
              <span v-if="row.type === '2'">自营</span>
              <span v-if="row.type === '3'">临时</span>
              <span v-if="row.type === '4'">期初</span>
            </template>
          </el-table-column>
          <el-table-column prop="commodityNum" label="订单数量" align="center" min-width="80" />
          <el-table-column prop="returnedCount" label="已退数量" align="center" min-width="80" />
          <el-table-column prop="refundableCount" label="可退数量" align="center" min-width="80" />
          <el-table-column prop="returnsCount" label="退货数量" align="center" min-width="80" />
          <el-table-column prop="returnsMoney" label="点采价(总)" align="center" min-width="120" />
          <el-table-column prop="batchNumber" label="批次号" align="center" min-width="140" />

        </el-table>
      </el-form>
      <div slot="footer">
        <el-button class="celBtn" @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getShopEasy } from "@/api/shop";
import { getDictValueAndNameSource } from "@/api/dictionary";
import { getReimburseList, getReimburseDetail } from "@/api/finance";
export default {
  name: "IndependentAccount",
  data() {
    return {
      dialogTableVisible: false,
      sForm: {
        storeId: "",
        returnsNumber: "",
        status: ''
      },
      brandList: [],
      accountList: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      date: [],
      gridData: [],
      dialogForm: {
        storeName: "",
        returnsReason: "",
        returnsTime: "",
        divideCommodityVoList: [],
      },
      isSearch: false,
      returnsReasonList: [],
    };
  },
  computed: {
    openTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[0];
      } else {
        return "";
      }
    },
    endTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[1];
      } else {
        return "";
      }
    },
    returnsReason() {
      for (let key in this.returnsReasonList) {
        if (this.dialogForm.returnsReason == key) {
          return this.returnsReasonList[key];
        }
      }
    },
  },
  created() {
    let { query } = this.$route;
    if (this.$route.query.id) {
      this.sForm.storeId = query.id;
      this.date = [query.openTime, query.endTime];
      this.isSearch = true;
    }
    // this.getShopEasy();
    this.getReimburseList();
    this.getReturnsReasonList();
  },
  methods: {
    async getReturnsReasonList() {
      const res = await getDictValueAndNameSource("RETURNS_REASON");
      if (res.code === 200) {
        this.returnsReasonList = res.data;
        console.log(this.returnsReasonList, "this.returnsReasonList");
      }
    },
    // 获取门店列表
    async getShopEasy() {
      const res = await getShopEasy();
      if (res.code === 200) {
        this.brandList = res.data.store;
      }
    },
    // 查找列表数据
    async getReimburseList() {
      const { openTime, endTime } = this;
      if (!this.isSearch) {
        this.sForm.returnsNumber = "";
        this.sForm.storeId = "";
        this.sForm.status = "";
        this.data = [];
      }
      const res = await getReimburseList({
        ...this.page,
        ...this.sForm,
        openTime,
        endTime,
      });
      if (res.code === 200) {
        console.log(res.data, "data");
        this.accountList = res.data.list;
        this.page.total = res.data.total;
        this.page.pageNum = res.data.pageNum;
        this.page.pageSize = res.data.pageSize;
        const $table = this.$refs.xTable1
        $table.loadData(this.accountList);
      }
    },
    // 搜索方法
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.getReimburseList();
    },
    // 重置
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.sForm.returnsNumber = "";
      this.sForm.storeId = "";
      this.sForm.status = "";
      this.date = [];
      this.getReimburseList();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;

      this.getReimburseList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;

      this.getReimburseList();
    },
    async detail(row) {
      const res = await getReimburseDetail({
        id: row.id,
      });
      if (res.code === 200) {
        console.log(res.data, "res");
        this.dialogForm.storeName = res.data.storeName;
        this.dialogForm.returnsReason = res.data.returnsReason;
        this.dialogForm.returnsTime = res.data.returnsTime;
        this.gridData = res.data.commodityList;
      }
      this.dialogTableVisible = true;
    },
    close() {
      this.dialogForm.storeName = "";
      this.dialogForm.returnsReason = "";
      this.dialogForm.returnsTime = "";
      this.gridData = [];
      this.dialogTableVisible = false;
    },
  },
};
</script>

<style scoped lang="scss"></style>
